package com.xueyl.springboot.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.websocket.server.PathParam;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.xueyl.springboot.domain.User;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;

@RestController
@RequestMapping(value = "/users")
@Api(value = "用户api接口", description="用于获取用户相关信息接口")
public class UserController {
	
	static Map<Long, User> users = Collections.synchronizedMap(new HashMap<>());

	@ApiOperation(value="获取用户列表")
	@ApiResponse(code=200,message="json")
	@RequestMapping(value="", method=RequestMethod.GET)
	public List<User> getUserList(){
		List<User> result = new ArrayList<>(users.values());
		return result;
	}
	@ApiOperation(value="创建用户", notes="根据User对象创建用户信息")
	@ApiImplicitParam(name="user", value="用户详细实体User对象", required=true, dataType="User")
	@RequestMapping(value="", method=RequestMethod.POST)
	public String addUser(@ModelAttribute User user){
		users.put(user.getId(), user);
		return "success";
	}
	@ApiOperation(value= "获取用户信息", notes="根据用户id获取用户信息")
	@ApiImplicitParam(name="id", value="用户id", required=true, dataType="Long")
	@RequestMapping(value="/{id}", method=RequestMethod.GET)
	public User getUser(@PathVariable("id") Long id){
		return users.get(id);
	}
	
	@ApiOperation(value="删除用户信息", notes="根据用户id删除用户信息")
	@ApiImplicitParam(name="id", value="用户id", required=true, dataType="Long")
	@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
	public String deleteUser(@PathVariable Long id){
		users.remove(id);
		return "success";
	}
	@ApiOperation(value="修改用户信息", notes="根据用户id修改用户信息")
	@ApiImplicitParams(value={
			@ApiImplicitParam(name="id", value="用户id", required=true, dataType="Long"),
			@ApiImplicitParam(name="user", value="需要更新的用户信息", required=true, dataType="User")
	})
	@RequestMapping(value="/{id}",method=RequestMethod.PUT)
	public String putUser(@PathVariable Long id, @ModelAttribute User user){
		users.put(id, user);
		return "success";
	}
}
